// /Users/wanghongsheng/工作/miniprograms/ShortPlayerMiniApp/pages/mine/mine.js
import {
    APP_SHORT_NAME,
    IM_ID
} from '../../config';
import {
    loginController
} from '../../utils/login'
import {
    getIconBalance
} from '../../api/modules/payment'
Page({
    data: {
        /**
         * {
         * name:'xxx',
         * phone:'183****5678',
         *}
         */
        user: null,
        balanceInfo: null,
        isLogin: false,
        short_name: APP_SHORT_NAME,
        recharge_label: "充值",
        imId: IM_ID
    },
    timer: null,
    async onShow() {
        await this.login();
        this._fetchBalance()
    },
    async login() {
        await loginController.login(true)
        this.setData({
            isLogin: loginController.isDeveloperServerLogin(),
            user: loginController.getUserInfo()
        })
    },
    async tryLogin() {
        await this.login()
        // tt.showToast({
        //   title: "请先登录",
        //   icon: "none"
        // });
    },
    async toHistory() {
        if (!loginController.isDeveloperServerLogin()) {
            await this.tryLogin();
        }
        tt.navigateTo({
            url: "/pages-mine/watchHistory/watchHistory"
        });
    },
    async toLiked() {
        if (!loginController.isDeveloperServerLogin()) {
            await this.tryLogin();
        }
        tt.navigateTo({
            url: "/pages-mine/like/like"
        });
    },
    async toRecharge() {
        if (!loginController.isDeveloperServerLogin()) {
            await this.tryLogin();
        }
        tt.navigateTo({
            url: "/pages-mine/recharge/recharge"
        });
    },
    async toOrderHistory() {
        if (!loginController.isDeveloperServerLogin()) {
            await this.tryLogin();
        }
        tt.navigateTo({
            url: "/pages-mine/orderHistory/orderHistory"
        });
    },
  
    _fetchBalance() {
        if (this.timer) {
            this.timer = clearTimeout(this.fetchBalance)
        }
        this.timer = setTimeout(() => {
            this.fetchBalance();
        }, 0);
    },
    async fetchBalance() {
        const {
            data
        } = await getIconBalance();
        // console.log(data)
        this.setData({
            balanceInfo: data
        })
        this.timer = null;
    },
    imCallback(e) {
        console.log("跳转IM客服成功", e.detail);
    },
    onimError(e) {
        console.log("拉起IM客服失败", e.detail);
        tt.showToast({
            icon: 'fail',
            title: `拉起IM客服失败`
        });
    },
    logout() {
        tt.showModal({
            title: "提醒",
            content: "确认登出？",
            success: (res) => {
                if (res.confirm) {
                    loginController.logout();
                    this.setData({
                        user: null,
                        isLogin: false,
                        balanceInfo: null
                    })
                }
            },
            fail: (res) => {

            },
        });
    }
})